package com.zhaofan.medium;

import java.math.BigDecimal;
import java.util.*;

public class No515 {

    public static List<Integer> largestValues(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        ArrayList<Integer> result = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        queue.offer(null);
        TreeSet<Integer> level = new TreeSet<>((Integer v1, Integer v2) -> {
            if (v1 < v2) {
                return 1;
            } else if (v1 == v2) {
                return 0;
            } else {
                return -1;
            }
        });
        int max = 0;
        int lastVal = 0;
        while (queue.size() != 0) {
            TreeNode tn = queue.poll();
            if (tn == null) {
                if (queue.size() != 0) {
                    queue.offer(null);
                }
                result.add(level.pollFirst());
                level.clear();
                continue;
            }
            level.add(tn.val);
            if (tn.left != null) {
                queue.offer(tn.left);
            }
            if (tn.right != null) {
                queue.offer(tn.right);
            }
        }
        return result;
    }

    public static void main(String[] args) {
//        TreeNode v5 = new TreeNode(5);
//        TreeNode v3 = new TreeNode(3);
//        TreeNode v9 = new TreeNode(9);
//        TreeNode v3_2 = new TreeNode(-2147483648);
//        TreeNode v2 = new TreeNode(2147483647);
//        TreeNode v1 = new TreeNode(0,v3_2,v2);
//        System.out.println(largestValues(v1));
        TreeSet<Integer> level = new TreeSet<>((Integer v1, Integer v2) -> {
            if (v1 < v2) {
                return 1;
            } else if (v1 == v2) {
                return 0;
            } else {
                return -1;
            }
        });
        level.addAll(Arrays.asList(1, 11, 4, 5, 6));
        System.out.println(level);
    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
